package com.tabbinder.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestBuilder;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HorizontalSplitPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point class to set up high-level widgets
 * 
 * @author Tatiana Kichkaylo
 */
public class MasterBind implements EntryPoint {
	private ListPanel listPanel;
	private HeaderPanel headerPanel;
	private LinksPanel linksPanel;

	public void onModuleLoad() {

		listPanel = new ListPanel(this);
		headerPanel = new HeaderPanel(this);
		linksPanel = new LinksPanel(this);

		// Detach the panels from where they are and put them into split panel.
		// Do this only if there is a hook for it, otherwise leave things as is
		RootPanel mainPanel = RootPanel.get("splitPanel");
		if (mainPanel != null) {
			HorizontalSplitPanel splitPanel = new HorizontalSplitPanel();
			splitPanel.setSplitPosition("30%");
			RootPanel left = RootPanel.get("leftPanel");
			splitPanel.setLeftWidget(left);
			RootPanel right = RootPanel.get("rightPanel");
			splitPanel.setRightWidget(right);
			mainPanel.add(splitPanel);
		}

		final Label userName = new Label();
		RootPanel.get("mainTitle").add(userName);

		final Anchor logoutLink = new Anchor("Logout");
		RootPanel.get("logoutLink").add(logoutLink);

		RequestBuilder getUserBuilder = new RequestBuilder(RequestBuilder.GET,
				Constants.USER_URL);
		getUserBuilder.setCallback(new RequestCallback() {
			public void onError(Request request, Throwable exception) {
				// Whatever
			}

			public void onResponseReceived(Request request, Response response) {
				if (Constants.STATUS_CODE_OK == response.getStatusCode()) {
					// There should be two lines: 1st name, 2nd logout URL
					String text = response.getText();
					String[] parts = text.split("\n");
					if (parts.length >= 3) {
						String name = parts[0]; // nickname
						if ("".equals(name))
							name = parts[1]; // email
						userName.setText("You are logged in as " + name);
						logoutLink.setHref(parts[2]);
					} else {
						Window.alert("Got weird response to get user: " + text);
					}
				} else if (Constants.STATUS_CODE_UNAUTHORIZED == response
						.getStatusCode()) {
					// login required
					Window.open(response.getText(), "_self", "");
				}
			}
		});
		try {
			getUserBuilder.send();
		} catch (RequestException e) {
			Window.alert(e.toString());
		}
	}

	/** Item clicked in the list of available binders */
	public void itemSelected(ListItem item) {
		listPanel.selectItem(item);
		headerPanel.selectItem(item);
		linksPanel.selectItem(item);
	}

	public void submitUpdate(ListItem item) {
		listPanel.submitUpdate(item);
	}

	public void itemRemoved(ListItem item) {
		listPanel.removeItem(item);
	}
}